﻿using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using WPFExcelReport.Controls;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class UIAM : ViewBase
    { 

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwAM = dwMaster;
            if (dwMaster == null)
            {
                KEY = "";
                AMBDEMA = "";
                MACHINEID = ""; 
            }
            else
            {
                KEY = TMV.AMNRKRS + TMV.AMLFDNR + TMV.AMPOS + TMV.AMTYP + TMV.AMART + TMV.AMKSTKRS + TMV.AMKST;
                MACHINEID = TMV.AMNRKRS + TMV.AMLFDNR + TMV.AMPOS;
                AMBDEMA = TMV.AMBDEMA;
            }
        }  
        public UIAM()
        {

        } 
        ~UIAM()
        {

        } 
        public void Insert(DataRow _dwAM)
        {
            TMV.InserAMSTAMM(_dwAM, app.company);
            TBMain.Rows.InsertAt(_dwAM, 0);
        }
        public void ResetSeq(object sender, RoutedEventArgs e)
        {
            if (!TMV.IsITX)
            {
                return;
            }
            dwData = TBMain.NewRow();
            dwData["AMMANDANT"] = app.Mandant;
            TMV.DAM00(dwData);

            dwData = TMV.VMOD.TBMain.NewRow();
            dwData["AMMMANDANT"] = app.Mandant;
            TMV.VMOD.DAMD00(dwData);

            dwData = TMV.VME.TBMain.NewRow();
            dwData["AMEMANDANT"] = app.Mandant;
            TMV.VME.DAME00(dwData);

            dwData = TMV.VMEK.TBMain.NewRow();
            dwData["MEKMANDANT"] = app.Mandant;
            TMV.VMEK.DMAE00(dwData);

            dwData = TMV.VMEV.TBMain.NewRow();
            dwData["MEVMANDANT"] = app.Mandant;
            TMV.VMEV.DMER00(dwData);
        }
        public virtual void StartMonitoring(bool IsSimulate)
        {
            if (TMV.AMClient == null)
            {
                return;
            }
            TMV.AMClient.MachinesLib[MKEY] = this;
            TMV.AMClient.Register("REG", MKEY, this);
            if(IsSimulate)
            {
                TMV.AMClient.Register("SIMULATE", MKEY, this);
            } 
            if (false)
            {
                dwData = TBMain.NewRow();
                dwData["AMMANDANT"] = app.Mandant;
                dwData["AMNRKRS"] = MKEY.Substring(0, 2);
                dwData["AMLFDNR"] = MKEY.Substring(2, 4);
                dwData["AMPOS"] = MKEY.Substring(6, 2);
                dwData["AMFILLER"] = TMV.AMClient.EndPoint.ToString().PadRight(106);
                TMV.UAM04(dwData);
            }
        }
        public virtual void StartMonitoring()
        {
            if (!IsMonitoring)
                return;
            StartMonitoring(false);
        }
    } 
}
